package com.fitns.user.controller.baseConfig;

import com.fitns.user.conf.Constants;
import com.fitns.user.dto.baseConfig.TbFacilityTypeDTO;
import com.fitns.user.dto.baseConfig.TbFacilityTypeVO;
import com.fitns.user.model.baseConfig.TbFacilityType;
import com.fitns.user.service.pub.FileService;
import com.fitns.user.utils.UuidUtils;
import com.xiaoleilu.hutool.lang.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fitns.user.conf.Resp;
import com.fitns.user.service.baseConfig.TbFacilityTypeService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin //解决前后端对接跨域问题
@RequestMapping("facilityType")
@Api(description="数据类型")
public class TbFacilityTypeController {

	@Autowired
	private TbFacilityTypeService tbFacilityTypeService;

	@Autowired
	private FileService fileService;

  /* @PostMapping("insert/FaciliType")
	@ApiOperation("新增数据类型")
	public Object insertFacilityType(@Validated TbFacilityTypeDTO tbFacilityTypeDTO) {
		int insert =  tbFacilityTypeService.insertFacilityType(tbFacilityTypeDTO);
		Resp resp = null;
		if(insert!=0) {
			resp = Resp.wrap("数据类型添加成功");
		}else {
			 resp = Resp.errorWrap("数据类型添加失败");
		}
		return resp;
	}
*/
	

  	@ApiOperation("新增数据类型")
   	@PostMapping("insert/FaciliType")
   	public Object insertFacilityType(@Validated TbFacilityTypeDTO tbFacilityTypeDTO,String facilityType) {
	Resp resp = null;
   		int insert =  tbFacilityTypeService.insertFacilityType(tbFacilityTypeDTO, facilityType);
   		
   		if(insert == 1) {
   			resp = Resp.wrap("数据类型添加成功");
   			return resp;
   		}else if(insert == 2){
   			resp = Resp.errorWrap("数据类型添加失败");
   			return resp;
   		}else if(insert == 3) {
   			resp = Resp.wrap("该类型名称已存在，不能被添加");
   			return resp;
   		}
   		return resp;
   	}
   
	
	
	@ApiOperation("修改数据类型，设施类型为不可选状态，不可修改")
	@PostMapping("update/FaciliType")
	public Object updateFacilityType(@Validated TbFacilityTypeDTO tbFacilityTypeDTO) {
		int update =  tbFacilityTypeService.updateFacilityType(tbFacilityTypeDTO);
		Resp resp = null;
		if(update!=0) {
			resp = Resp.wrap("修改信息成功");
		}else {
			 resp = Resp.errorWrap("修改信息失败");
		}
		return resp;
	}

	@ApiOperation("删除数据类型，物理删除，根据id删除")
	@PostMapping("delete/FaciliType")
	public Object deleteFacilityTypeById(@RequestParam long id) {
		int delete =  tbFacilityTypeService.deleteFacilityTypeById(id);
		Resp resp = null;
		if(delete!=0) {
			resp = Resp.wrap("删除成功");
		}else {
			 resp = Resp.errorWrap("请稍后重试");
		}
		return resp;
	}

	@ApiOperation("查询数据类型，支持模糊查询类型字段")
	@PostMapping("select/FaciliType")
    public Object selectBannerList(@Validated TbFacilityTypeDTO tbFacilityTypeDTO) {
        PageInfo<TbFacilityTypeVO> select = tbFacilityTypeService.selectBannerList(tbFacilityTypeDTO);
        select.setPageSize(tbFacilityTypeDTO.getPageSize());
        return Resp.page(select);
    }

	@ApiOperation("查询所有设施类型")
	@PostMapping("select/AllType")
	public Object selectAllType(@Validated TbFacilityTypeDTO tbFacilityTypeDTO) {
		List<TbFacilityTypeVO> selectList = tbFacilityTypeService.selectAllType(tbFacilityTypeDTO);
		return selectList;
	}


	@ApiOperation("根据id查询数据类型信息")
	@PostMapping("select/ById")
	public Object selectTbFacilityById(@RequestParam long id) {
		TbFacilityType byId =  tbFacilityTypeService.selectTbFacilityById(id);
		return byId;
	}

	/** 图片上传功能 */
	@PostMapping("updateFile")
	@ApiOperation("上传图片并新增或修改相应信息")
	public Integer inserIcon(TbFacilityTypeDTO tbFacilityTypeDTO, MultipartFile pic) throws Exception {
		// 把图片保存到图片目录下
		// 为每个文件生成一个新的文件名
		if(Validator.isNotNull(pic)) {
			String picName = UuidUtils.getUUID();
			// 取文件扩展名
			String oriName = pic.getOriginalFilename();
			if(Validator.isNotNull(oriName)&&"".equals(oriName)){
				return tbFacilityTypeService.updateFacilityType(tbFacilityTypeDTO);
			}
			String extName = oriName.substring(oriName.lastIndexOf("."));
			// 保存文件
			pic.transferTo(new File(Constants.IMAGE_PATH + picName + extName));

			// 把文件名保存到数据库
			tbFacilityTypeDTO.setIcon(picName + extName);
			Integer icon = tbFacilityTypeService.inserIcon(tbFacilityTypeDTO);

			//int insert =  tbFacilityTypeService.insertFacilityType(tbFacilityTypeDTO);
			return icon;
		}
		return tbFacilityTypeService.updateFacilityType(tbFacilityTypeDTO);

	}
	/**
	  * IO流读取图片 by:long
	  * @return
	  */
	@GetMapping("/downLoadImage")
	@ApiOperation("下载图片")
	public String IoReadImage(String imgName, HttpServletRequest request, HttpServletResponse response) throws IOException {
		return fileService.IoReadImage(imgName, request, response);
	}

}
